package org.plex.core.imp;

import java.util.ArrayList;
import java.util.List;

import org.plex.core.TaskQueueManager;
import org.plex.model.User;
import org.plex.model.imp.UserImp;
import org.plex.queue.GroupQueue;
import org.plex.queue.UserQueue;
import org.plex.queue.imp.GroupQueueImp;
import org.plex.queue.imp.UserQueueImp;

import com.adobe.idp.dsc.clientsdk.ServiceClientFactory;
import com.adobe.idp.taskmanager.dsc.client.TaskManagerClientFactory;
import com.adobe.idp.taskmanager.dsc.client.queuemanager.QueueManager;
import com.adobe.idp.taskmanager.dsc.client.queuemanager.QueueManagerException;
import com.adobe.idp.taskmanager.dsc.client.queuemanager.UserDef;
import com.adobe.idp.um.api.infomodel.impl.UserImpl;
import com.sun.xml.internal.messaging.saaj.packaging.mime.util.QEncoderStream;

public class TaskQueueManagerImp implements TaskQueueManager
{

	private ServiceClientFactory serviceClientFactory;
	private QueueManager queueManager;
	
	public TaskQueueManagerImp(ServiceClientFactory serviceClientFactory)throws NullPointerException
	{
		if(null==serviceClientFactory)
		{
			throw new NullPointerException();
		}
		this.serviceClientFactory=serviceClientFactory;
		this.queueManager=TaskManagerClientFactory.getQueueManager(serviceClientFactory);
	}
	
	
	
	public long createUserQueue(UserQueue userQueue) throws QueueManagerException
	{
		UserQueueImp userQueueImp=(UserQueueImp)userQueue;
	   return queueManager.createUserQueue(userQueueImp.getUserQueue());
	}

	public UserQueue getEmptyUserQueue()
	{
		
		return new UserQueueImp(queueManager.getEmptyUserQueue());
	}

	public GroupQueue getGroupQueue(String domainName, String groupCanonicalName) throws NullPointerException, QueueManagerException
	{
		return new GroupQueueImp(queueManager.getGroupQueue(domainName, groupCanonicalName));
	}

	public List<GroupQueue> getGroupQueuesForUser() throws QueueManagerException
	{
	    com.adobe.idp.taskmanager.dsc.client.queuemanager.GroupQueue[] groupQueues=this.queueManager.getGroupQueuesForUser();
	    List<GroupQueue> list=new ArrayList<GroupQueue>();
	    for(com.adobe.idp.taskmanager.dsc.client.queuemanager.GroupQueue  groupQueue:groupQueues)
	    {
	    	list.add(new GroupQueueImp(groupQueue));
	    }
		return list;
	}

	public List<UserQueue> getUserQueues() throws QueueManagerException
	{
		com.adobe.idp.taskmanager.dsc.client.queuemanager.UserQueue[] userQueues=queueManager.getUserQueues();
		List<UserQueue> list=new ArrayList<UserQueue>();
		for(com.adobe.idp.taskmanager.dsc.client.queuemanager.UserQueue userQueue: userQueues)
		{
		   list.add(new UserQueueImp(userQueue));
		}
		return list;
	}

	public void grantQueueAccess(long queueId, String userOidToGrant) throws QueueManagerException
	{
         queueManager.grantQueueAccess(queueId, userOidToGrant);
	}

	public List<User> listSharedUsersForQueue(long queueId) throws QueueManagerException
	{
		UserDef[] userDefs=queueManager.listSharedUsersForQueue(queueId);
		List<User> list=new ArrayList<User>();
		for(UserDef userDef:userDefs)
		{
			com.adobe.idp.um.api.infomodel.User user=new UserImpl();
			user.setOid(userDef.getId());
			user.setUserid(userDef.getUid());
			user.setGivenName(userDef.getGivenname());
			user.setFamilyName(userDef.getFamilyname());
			user.setEmail(userDef.getEmail());
			user.setTelephoneNumber(userDef.getPhone());
			user.setStatus(Short.toString(userDef.getStatus()));
			user.setCommonName(userDef.getCommonname());
			list.add(new UserImp(user));
		}
		return list;
	}

	public void removeUserQueue(long queueOidToRemone) throws QueueManagerException
	{
		  queueManager.removeUserQueue(queueOidToRemone);
	}

	public void revokeQueueAccess(long queueId, String userOidTorevoke) throws QueueManagerException
	{
		queueManager.revokeQueueAccess(queueId, userOidTorevoke);

	}

	public void setServiceClientFactory(ServiceClientFactory serviceClientFactory)
	{
		this.serviceClientFactory = serviceClientFactory;
	}

	public ServiceClientFactory getServiceClientFactory()
	{
		return serviceClientFactory;
	}

}
